<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function arr(s) {
      let ans = 0;
      for (let i = 0; i < s.length; ++i) {
        const cnt = new Map();
        for (let j = i; j < s.length; ++j) {
          cnt.set(s[j], (cnt.get(s[j]) || 0) + 1);
          const t = Array.from(cnt.values());
          ans += Math.max(...t) - Math.min(...t);
        }
      }
      return ans;
    };
    console.log(arr("aabcb"));
  </script>
</body>

</html>